
var vm = new Vue({
	el:'#rrapp',
	data:{
        // q:{
			// username: null
        // },
        isOver: false,
        // title:null,
        sumList: [{name:'', list: [{name:'', odds:'', money:''}]}],
        ballCategorys:[{name:'', list: [{name:'', odds:'', money:''}]}],
        specials: [{name:'', list:[{name:'', odds:'', money:''}]}],
        currentBottom: {}
	},
    methods: {
		query: function () {
            $.get(baseURL + "sys/sysbottom/info/", function(r){
                vm.sumList = r.sumList;
                vm.ballCategorys = r.ballCategorys;
                vm.specials = r.specials;

                if (r.currentBottom == null) {
                    vm.isOver = true;
                    return;
                }
                vm.currentBottom = r.currentBottom;

                var currentDate = new Date();
                var createTime = new Date(vm.currentBottom.createTime);
                var alreadyTime = currentDate.getTime() - createTime.getTime();

                var duration = vm.currentBottom.duration * 60;

                vm.awardRemainTime = duration - (alreadyTime / 1000);
                vm.endRemainTime = vm.awardRemainTime - 45;
                setInterval(function() {
                    vm.endRemainTime--;
                    if (vm.endRemainTime <= 0) {
                        vm.isOver = true;
                    }
                }, 1000);
            });
        },
		reset: function () {
            confirm("您下的注会全部重置，您确定要继续吗？", function(){
                vm.sumList.forEach(function (sum) {
                    sum.list.forEach(function (item) {
                        item.money = null;
                    });
                });
                vm.ballCategorys.forEach(function (ball) {
                    ball.list.forEach(function (item) {
                        item.money = null;
                    })
                });
                vm.specials.forEach(function (special) {
                    special.list.forEach(function (item) {
                        item.money = null;
                    })
                });
                alert("重置成功！");
            });
        },
		saveOrUpdate: function () {
            if(vm.validator()){
                return ;
            }

            confirm("您将要下注，确定继续吗？", function () {

                var bottomList = vm.getBottomList();
                if (bottomList.length == 0) {
                    alert("请先下注");
                    return;
                }
                if (vm.currentBottom == null || vm.currentBottom.bottomId == null) {
                    alert("该局暂时无法下注，请刷新重试！")
                    return;
                }
                var url = "sys/sysbottomuser/bottom?bottomId=" + vm.currentBottom.bottomId;
                $.ajax({
                    type: "POST",
                    url: baseURL + url,
                    contentType: "application/json",
                    data: JSON.stringify(bottomList),
                    success: function(r){
                        if(r.code === 0){
                            alert('操作成功', function(){
                                // vm.isOver = true;
                                parent.location.reload();
                            });
                        }else{
                            alert(r.msg);
                        }
                    }
                });
            });
        },
        getBottomList: function () {

            var result = [];

            vm.createBottomList(result, vm.sumList);

            vm.createBottomList(result, vm.ballCategorys);

            vm.createBottomList(result, vm.specials);

            return result;
        },
        createBottomList: function (result, paramList) {
            paramList.forEach(function (odds) {
                var sumList = [];
                odds.list.forEach(function (item) {
                    if (item.money && item.money > 0) {
                        sumList.push(item);
                    }
                });
                if (sumList.length > 0) {
                    result.push({name: odds.name, list: sumList});
                }
            });
        },
        validator: function () {

        }
    },
    mounted: function () {
        this.query();
    }
});